﻿#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using MCMLXVII.BackEnd.Services.SystemConfiguration.Messages;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.Client.Common.Session;


#endregion

namespace MCMLXVII.Tools.OrganizationChart
{
    partial class frmDirectoryService : Form
    {

        private bool TreeIsSynchronized = false;


        public frmDirectoryService()
        {
            InitializeComponent();
        }
        private string _BaseValue = "";
        public string BaseValue
        {
            get { return _BaseValue; }
            set { _BaseValue = value;}

        }

        private void frmDirectoryService_Load(object sender, EventArgs e)
        {
            cmbProviders.Items.Clear();
            tvwtree.Enabled = false;
            cmdConfirm.Enabled = false;
            foreach (msgSecurityProvider sec in StaticCache.SecurityProviders)
            {
                string item = sec.FQDN;
                cmbProviders.Items.Add(item);
                if ((_BaseValue != null) && _BaseValue.Contains("//" + item))
                {
                    TreeIsSynchronized = false;
                    cmbProviders.SelectedIndex = cmbProviders.Items.Count - 1;
                    TreeIsSynchronized = true;
                }
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void cmdConfirm_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void cmbProviders_SelectedIndexChanged(object sender, EventArgs e)
        {
            tvwtree.Nodes.Clear();
            tvwtree.Enabled = false;
            if (cmbProviders.SelectedIndex >= 0)
            {
                cmdConfirm.Enabled = false;
                string ActualVal = cmbProviders.Items[cmbProviders.SelectedIndex].ToString();
                foreach (msgSecurityProvider sec in StaticCache.SecurityProviders)
                {
                    if ((sec.FQDN.Length <= ActualVal.Length) && (sec.FQDN == ActualVal.Substring(0, sec.FQDN.Length)))
                    {
                        if (sec.SupportsTreeBrowsing)
                        {

                            //TODO: Add try catch here
                            msgXMLResponse res =  BusinessActions.GetDirectoryTree(sec.SecProviderID);
                            XmlNode node = res.Document.SelectSingleNode("Root");

                            if (node != null)
                            {
                                TreeNode NewNode =  tvwtree.Nodes.Add(node.Attributes.GetNamedItem("Path").Value , node.Attributes.GetNamedItem("Name").Value );
                                NewNode.Tag = node.Attributes.GetNamedItem("Expanded").Value;
                                if ((_BaseValue != null) && (!TreeIsSynchronized) && (_BaseValue == node.Attributes.GetNamedItem("Path").Value))
                                    tvwtree.SelectedNode = NewNode;
                                LoadNodes(node, NewNode);
                            }
                            tvwtree.Enabled = true;
                            tvwtree.Select();
                        }
                        else
                            cmdConfirm.Enabled = true;
                    }
                }
            }
            else
            {
                cmdConfirm.Enabled = false;
            }
        }
        private void LoadNodes(XmlNode node, TreeNode tvwNode)
        {
            TreeNode NewNode;
            foreach (XmlNode nod in node.ChildNodes)
            {
                NewNode = tvwNode.Nodes.Add(nod.Attributes.GetNamedItem("Path").Value , nod.Attributes.GetNamedItem("Name").Value );
                NewNode.Tag = nod.Attributes.GetNamedItem("Expanded").Value;
                if ((_BaseValue != null) && (!TreeIsSynchronized) && (_BaseValue == nod.Attributes.GetNamedItem("Path").Value))
                {
                    NewNode.EnsureVisible();
                    tvwtree.SelectedNode = NewNode;
                }
                if (nod.HasChildNodes)
                    LoadNodes(nod, NewNode);
            }
        }

        private void tvwtree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null)
            {
                cmdConfirm.Enabled = true;
                _BaseValue = e.Node.Name;
            }
        }

        private void tvwtree_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (tvwtree.SelectedNode != null)
            {
                _BaseValue = tvwtree.SelectedNode.Name;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }

}